<template>
	<view class="page">
		<flowbefore :beflows="beflows" :status="status" :isBtnClick="isBtnClicks" @save="save" @submit="submit" :statusType="statusType">
			<template>
				<view class="padding-bottom-100">
					<form class="flowform">
						<view class="formlist inputtext" v-if="$formOperates.read('flowTitle',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>流程标题</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="document.flowTitle" name="flowTitle" :disabled="judge('flowTitle')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('billNo',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>单号编号</label></view>
								<view class="controlvalue">
									<input type="text" v-model="document.billNo" name="billNo" :disabled="judge('billNo')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="flowUrgent" v-if="$formOperates.read('flowUrgent',formOperatesData)">
							<view class="formgroup">
								<view class="labletitle"><label>紧急程度</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="document.flowUrgent" name="flowUrgent" placeholder="选择"/>
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext margin-top-20" v-if="$formOperates.read('fileName',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>文件名称</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="document.fileName" name="fileName" placeholder="输入文件名称" :disabled="judge('fileName')"/>
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('draftedPerson',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>拟稿人</label></view>
								<view class="controlvalue">
									<input type="text" v-model="document.draftedPerson" name="draftedPerson" placeholder="输入拟稿人" :disabled="judge('draftedPerson')"/>
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('serviceUnit',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>发文单位</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="document.serviceUnit" name="serviceUnit" placeholder="输入发文单位" :disabled="judge('serviceUnit')"/>
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('fillPreparation',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>文件拟办</label></view>
								<view class="controlvalue">
									<input type="text" v-model="document.fillPreparation" name="fillPreparation" placeholder="输入文件拟办" :disabled="judge('fillPreparation')"/>
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('fillNum',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>文件编号</label></view>
								<view class="controlvalue">
									<input type="text" v-model="document.fillNum" name="fillNum" placeholder="输入文件编号" :disabled="judge('fillNum')"/>
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="receiptDate" v-if="$formOperates.read('receiptDate',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>收文日期</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="document.receiptDate" name="receiptDate" placeholder="选择"/>
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist" v-if="$formOperates.read('modifyOpinion',formOperatesData)">
							<view class="formgroup  padding-17">
								<view class="labletitletar"><label>修改意见</label></view>
								<view class="controlvalue  uni-textarea">
									<textarea v-model="document.modifyOpinion" name="modifyOpinion" maxlength="500" placeholder="输入" :disabled="judge('modifyOpinion')"/>
								</view>
							</view>
						</view>
						<view class="formlist" v-if="$formOperates.read('fileJson',formOperatesData)">
							<JNPFUpload v-model="fileList" :fileSize='2' :limit='9'></JNPFUpload>
						</view>
					</form>
					<view class="height-100"></view>
				</view>
			</template>
		</flowbefore>
		<w-picker :mode="mode" :pickerList="pickerList" ref="picker" :isSearchShow="isSearchShow"></w-picker>
	</view>
</template>

<script>
	import jnpf from '@/common/js/jnpf.js'
	import helper from '@/common/js/helper.js'
	import graceChecker from '@/common/js/graceChecker.js'
	import wPicker from '@/components/w-picker/w-picker.vue'
	import flowbefore from '@/components/work/flow-before.vue'
	import config from '@/common/js/config.js'
	let formRule=[
			{name:"flowTitle",checkType:"notnull",checkRule:"",errorMsg:"输入标题"},
			{name:"flowUrgent", checkType : "notnull", checkRule:"",  errorMsg:"选择紧急程度"},
			{name:"fileName", checkType : "notnull", checkRule:"",  errorMsg:"输入文件名称"},
			{name:"serviceUnit", checkType : "notnull", checkRule:"",  errorMsg:"输入发文单位"},
			{name:"receiptDate", checkType : "notnull", checkRule:"",  errorMsg:"选择收文日期"},
		];
	export default {
		components: {
			wPicker,
			flowbefore,
		},
		data() {
			return {
				mode:'',//底部弹窗分类
				isSearchShow:false,
				disabled:false,//启用
				status:'',//流程状态
				beflows:{
					operstate:"",
					processId:''
				},
				document:{
					billNo:'',
					flowUrgent:'',
					flowTitle:jnpf.userInfo().realName+'的文件签批意见表',
					receiptDate:'',
					fileJson:'',
					serviceUnit:'',
					fileName:''
				},
				fileList:[],
				
				isBtnClicks:false,
				pickerList:[],
				urgentList:[{
					label: "普通",
					value:"1"
				},
				{
					label: "重要",
					value:"2"
				},
				{
					label: "紧急",
					value:"3"
				}],
				statusType:'',
				formOperatesData:[],
				F_Status: 0
			}
		},
		
		
		onLoad(e) {
			/* 工作区进入状态 */
			let statusType = uni.getStorageSync("type")
			this.statusType = statusType
			let F_Status = uni.getStorageSync('F_Status')
			if(F_Status == 1 || F_Status == 2){
				this.disabled = true;
			}
			/* 工作区进入状态 End*/
			let flowInfo= JSON.parse(e.flowInfo);
			this.beflows.operstate=flowInfo.operstate;
			if(flowInfo.operstate =='add'){
				this.document.flowId=flowInfo.id;
				this.status = 0;
				uni.setStorageSync('F_Status', 0);
				this.F_Status = Number(uni.getStorageSync('F_Status'))
				this.document.flowUrgent=helper.getSpinnerNew(this.urgentList,'1');
				helper.ajax({
					url: '/api/system/Base/BillRule/BillNumber/WF_DocumentApprovalNo',
					success: res => {
						this.document.billNo=res.data.data;
					}
				});
				this.getOperatesData(flowInfo.id)
			}
			if(flowInfo.operstate=='info'){
				uni.setStorageSync('F_Status', F_Status);
				this.F_Status = Number(uni.getStorageSync('F_Status'))
				this.disabled=flowInfo.disabled;
				this.beflows.processId=flowInfo.id;
				this.status=flowInfo.status;
				helper.ajax({
					url: '/api/workflow/Form/DocumentApproval/' + flowInfo.id,
					success: res => {
						let _document=res.data.data;
						this.document = _document;
						this.document.flowUrgent=helper.getSpinnerNew(this.urgentList,_document.flowUrgent);
						this.document.receiptDate=helper.toDate(_document.receiptDate,  "yyyy-MM-dd HH:mm");
						if (_document.fileJson!="") {
							this.fileList=JSON.parse(_document.fileJson);
						}
					}
				});
				if(this.status == 0 || this.status == 3){
					this.getOperatesData(flowInfo.flowId)
				}else{
					this.$eventHub.$off('formOperates');
					this.$eventHub.$on('formOperates', (data) => {
						this.formOperatesData = data;
						console.log(this.formOperatesData)
						uni.setStorageSync('formOperatesData', data)
					})
				}
			}
		},
		methods: {
			getOperatesData(id){
				helper.ajax({
					url: '/api/workflow/Engine/FlowEngine/'+id,
					success: res => {
						let kk = JSON.parse(res.data.data.flowTemplateJson);
						this.formOperatesData = [];
						this.formOperatesData = kk.properties.formOperates
						uni.setStorageSync('formOperatesData',this.formOperatesData)
					}
				});
			},
			//判断表单权限可写
			judge(id) {
				return this.statusType == 'backlog' ? this.$formOperates.write(id, this.formOperatesData) : this.F_Status ==
					1 || this.F_Status == 2 ? true : this.$formOperates.write(id, this.formOperatesData)
			},
			flowUrgent(e){
				uni.hideKeyboard();//隐藏软键盘  
				let isWrite = this.judge('flowUrgent')
				if(!isWrite){
					this.pickerList = this.urgentList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.document.flowUrgent = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			receiptDate(e){
				uni.hideKeyboard();//隐藏软键盘  
				let isWrite = this.judge('receiptDate')
				if(!isWrite){
					this.mode="dateTime";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.document.receiptDate = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			submit(e){
				this.document.fileJson = JSON.stringify(this.fileList)
				let formData=this.document;
				//根据表单权限过滤必填列表
				formRule = graceChecker.filterRequiredList(formRule,this.formOperatesData)
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return;
				}
				this.isBtnClicks=true;
				formData.flowUrgent=helper.setSpinnerNew(this.urgentList,formData.flowUrgent);
				formData.status = 0;
				let method = "POST";
				let url = "/api/workflow/Form/DocumentApproval";
				if(formData.id){
					method = "PUT";
					url=url+"/"+formData.id
				}
				formData.receiptDate = Date.parse(formData.receiptDate);
				helper.ajax({
					url: url,
					method:method,
					contentType: "application/json",
					data:formData,
					success: res => {
						helper.msg(res.data.msg, 'success');
						if (this.beflows.operstate=='add') {
							uni.redirectTo({
								url: '../../operate/started/started',
								success: res => {},
								fail: () => {},
								complete: () => {}
							});
						}else{
							uni.navigateBack();
						}
						this.isBtnClicks=false
					}
				});
			},
			save(e){
				this.document.fileJson = JSON.stringify(this.fileList)
				let formData=this.document;
				//根据表单权限过滤必填列表
				formRule = graceChecker.filterRequiredList(formRule,this.formOperatesData)
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return;
				}
				this.isBtnClicks=true;
				formData.flowUrgent=helper.setSpinnerNew(this.urgentList,formData.flowUrgent);
				formData.status = 1;
				let method = "POST";
				let url = "/api/workflow/Form/DocumentApproval";
				if(formData.id){
					method = "PUT";
					url=url+"/"+formData.id
				}
				formData.receiptDate = Date.parse(formData.receiptDate);
				helper.ajax({
					url: url,
					method:method,
					contentType: "application/json",
					data:formData,
					success: res => {
						helper.msg(res.data.msg, 'success');
						if (this.beflows.operstate=='add') {
							uni.redirectTo({
								url: '../../operate/started/started',
								success: res => {},
								fail: () => {},
								complete: () => {}
							});
						}else{
							uni.navigateBack();
						}
						this.isBtnClicks=false
					}
				});
			},
			
		},
		
	}
</script>

<style>
	.required{
		color: red;
	}
</style>
